using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace Logging
{
    public class SmtpHandler : Handler
    {

        private List<string> emailToAddresses;
        private string emailFromAddress;
        private string emailSubject;
        private string smtpHost;
        private int smtpPort = 25;

        public SmtpHandler(string smtpHost, string emailToAddress) : this(smtpHost,emailToAddress,emailToAddress,"SmtpLogger")
        {
        }

        public SmtpHandler(string smtpHost, string emailToAddress, string emailFromAddress, string emailSubject)
        {
            base.Level = Level.INFO;
            base.Encoding = "iso-8859-1";
            this.emailToAddresses = new List<string>();
            this.emailSubject = emailSubject;
            this.emailFromAddress = emailFromAddress;
            this.emailToAddresses.Add(emailToAddress);
            this.smtpHost = smtpHost;
            base.Formatter = new SimpleFormatter();
        }

        public SmtpHandler(string smtpHost, int smtpPort, List<string> emailToAddresses, string emailFromAddress, string emailSubject)
        {
            base.Level = Level.INFO;
            base.Encoding = "iso-8859-1";
            this.emailToAddresses = new List<string>(emailToAddresses);
            this.emailSubject = emailSubject;
            this.emailFromAddress = emailFromAddress;
            this.smtpPort = smtpPort;
            this.smtpHost = smtpHost;
            base.Formatter = new SimpleFormatter();
        }

        public string EmailSubject
        {
            get { return emailSubject; }
            set { emailSubject = value; }
        }

        public string EmailFromAddress
        {
            get { return emailFromAddress; }
            set { emailFromAddress = value; }
        }

        public List<string> EmailToAddresses
        {
            get { return emailToAddresses; }
            set { emailToAddresses = value; }
        }

        public string SmtpHost
        {
            get { return smtpHost; }
            set { smtpHost = value; }
        }

        public int SmtpPort
        {
            get { return smtpPort; }
            set { smtpPort = value; }
        }

        public override void Close()
        {
        }

        public override void Flush()
        {
        }

        public override void Publish(LogRecord record)
        {
            if (!IsLoggable(record)) return;
            SmtpClient smtp = new SmtpClient(smtpHost, smtpPort);
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(emailFromAddress);
            foreach (string emailToAddress in emailToAddresses)
            {
                mail.To.Add(new MailAddress(emailToAddress));
            }
            mail.SubjectEncoding = System.Text.Encoding.GetEncoding(Encoding);
            mail.Subject = emailSubject;
            mail.BodyEncoding = System.Text.Encoding.GetEncoding(Encoding);
            mail.Body = Formatter.Format(record);
            smtp.Send(mail);
        }
    }
}
